package com.love.driver.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.love.common.core.utils.DateUtils;
import com.love.common.core.utils.snow.SnowflakeIdGenerator;
import com.love.common.security.utils.SecurityUtils;
import com.love.driver.enums.DriverOrderEnum;
import com.love.driver.vo.DriverVo;
import com.love.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.love.common.log.annotation.Log;
import com.love.common.log.enums.BusinessType;
import com.love.common.security.annotation.RequiresPermissions;
import com.love.driver.domain.LoveDriverOrder;
import com.love.driver.service.ILoveDriverOrderService;
import com.love.common.core.web.controller.BaseController;
import com.love.common.core.web.domain.AjaxResult;
import com.love.common.core.utils.poi.ExcelUtil;
import com.love.common.core.web.page.TableDataInfo;

/**
 * 代驾订单主Controller
 * 
 * @author love
 * @date 2025-09-13
 */
@RestController
@RequestMapping("/order")
public class LoveDriverOrderController extends BaseController {

    //引用雪花算法
    private final SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(2, 2);

    @Autowired
    private ILoveDriverOrderService loveDriverOrderService;

    /**
     * 查询代驾订单主列表
     */
    @RequiresPermissions("driver:order:list")
    @GetMapping("/list")
    public TableDataInfo list(LoveDriverOrder loveDriverOrder)
    {
        startPage();
        List<LoveDriverOrder> list = loveDriverOrderService.selectLoveDriverOrderList(loveDriverOrder);
        return getDataTable(list);
    }

    /**
     * 导出代驾订单主列表
     */
    @RequiresPermissions("driver:order:export")
    @Log(title = "代驾订单主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoveDriverOrder loveDriverOrder)
    {
        List<LoveDriverOrder> list = loveDriverOrderService.selectLoveDriverOrderList(loveDriverOrder);
        ExcelUtil<LoveDriverOrder> util = new ExcelUtil<LoveDriverOrder>(LoveDriverOrder.class);
        util.exportExcel(response, list, "代驾订单主数据");
    }

    /**
     * 获取代驾订单主详细信息
     */
    @RequiresPermissions("driver:order:query")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(loveDriverOrderService.selectLoveDriverOrderByOrderId(orderId));
    }

    /**
     * 新增代驾订单主
     */
    @RequiresPermissions("driver:order:add")
    @Log(title = "代驾订单主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoveDriverOrder loveDriverOrder)
    {
        return toAjax(loveDriverOrderService.insertLoveDriverOrder(loveDriverOrder));
    }

    /**
     * 修改代驾订单主
     */
    @RequiresPermissions("driver:order:edit")
    @Log(title = "代驾订单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoveDriverOrder loveDriverOrder)
    {
        return toAjax(loveDriverOrderService.updateLoveDriverOrder(loveDriverOrder));
    }

    /**
     * 删除代驾订单主
     */
    @RequiresPermissions("driver:order:remove")
    @Log(title = "代驾订单主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Integer[] orderIds)
    {
        return toAjax(loveDriverOrderService.deleteLoveDriverOrderByOrderIds(orderIds));
    }

    /**
     * 新增代驾订单
     * @param loveDriverOrder
     * @return
     */
    @PostMapping("/addOrders")
    public AjaxResult submitOrder(@RequestBody LoveDriverOrder loveDriverOrder) {
        // 从上下文获取当前登录用户（由 Token 解析后自动注入）
        LoginUser currentUser = SecurityUtils.getLoginUser();
        Long userId = currentUser.getUserid(); // 获取用户ID
        //雪花算法生成全局唯一的订单ID
        Long orderId = idGenerator.nextId();
        loveDriverOrder.setOrderId(orderId);
        loveDriverOrder.setUserId(userId.longValue());
        loveDriverOrder.setOrderStatus(DriverOrderEnum.WAITING.getValue());
        return toAjax(loveDriverOrderService.insertLoveDriverOrder(loveDriverOrder));
    }

    /**
     * 用户查询自己订单
     */
    @GetMapping("/listUserOrders")
    public TableDataInfo listUserOrders(LoveDriverOrder loveDriverOrder)
    {
        // 从上下文获取当前登录用户（由 Token 解析后自动注入）
        LoginUser currentUser = SecurityUtils.getLoginUser();
        Long userId = currentUser.getUserid(); // 获取用户ID
        loveDriverOrder.setUserId(userId);
        List<LoveDriverOrder> list = loveDriverOrderService.selectUsersLoveDriverOrderList(loveDriverOrder);
        return getDataTable(list);
    }

    /**
     * 司机查询订单
     */
    @GetMapping("/listDriverOrders/{driverCode}")
    public TableDataInfo listDriverOrders(@PathVariable String driverCode)
    {
        List<DriverVo> list = loveDriverOrderService.selectDriversLoveDriverOrderList(driverCode);
        return getDataTable(list);
    }

    /**
     * 车主抢单
     * @param orderId
     */
    @PostMapping("/grabOrder/{orderId}")
    public void grabOrder(@PathVariable Long orderId){
        // 从上下文获取当前登录用户（由 Token 解析后自动注入）
        LoginUser currentUser = SecurityUtils.getLoginUser();
        Long userId = currentUser.getUserid(); // 获取用户ID
        LoveDriverOrder loveDriverOrder = new LoveDriverOrder();
        loveDriverOrder.setOrderId(orderId);
        loveDriverOrder.setDriverId(userId.longValue());
        loveDriverOrder.setOrderStatus(DriverOrderEnum.ACCEPTED.getValue());
        this.loveDriverOrderService.updateLoveDriverOrder(loveDriverOrder);
    }

    /**
     * 车主退单
     * @param orderId
     */
    @PostMapping("/cancelOrder/{orderId}")
    public void cancelOrder(@PathVariable Long orderId){
        LoveDriverOrder loveDriverOrder = new LoveDriverOrder();
        loveDriverOrder.setOrderId(orderId);
        this.loveDriverOrderService.updateOrderStatus(orderId);
    }

    /**
     * 司机完成
     * @param orderId
     */
    @PostMapping("/completeOrder/{orderId}")
    public void completeOrder(@PathVariable Long orderId){
        LoveDriverOrder loveDriverOrder = new LoveDriverOrder();
        loveDriverOrder.setOrderId(orderId);
        loveDriverOrder.setOrderStatus(DriverOrderEnum.COMPLETED.getValue());
        loveDriverOrder.setEndTime(DateUtils.getTime());
        this.loveDriverOrderService.updateLoveDriverOrder(loveDriverOrder);
    }

    /**
     * 用户取消
     * @param orderId
     */
    @PostMapping("/userCancelOrder/{orderId}")
    public void userCancelOrder(@PathVariable Long orderId){
        LoveDriverOrder loveDriverOrder = new LoveDriverOrder();
        loveDriverOrder.setOrderId(orderId);
        this.loveDriverOrderService.deleteLoveDriverOrderByOrderId(orderId);
    }

}
